﻿namespace Devonline.Entity;

/// <summary>
/// 证书, 字符串表达式的默认实现
/// 服务器颁发给客户端的证书, 用于校验客户端身份
/// </summary>
[Table("certificate"), DisplayName("证书")]
public class Certificate : Certificate<string>, IDateTimeRange, IEntitySet, IEntitySetWithCreate
{
    /// <summary>
    /// 构造方法给 Id 赋值
    /// </summary>
    public Certificate() => Id = KeyGenerator.GetStringKey();

    /// <summary>
    /// 用户个人信息
    /// </summary>
    public virtual Personal? Personal { get; set; }
    /// <summary>
    /// 客户端
    /// </summary>
    public virtual Client? Client { get; set; }
    /// <summary>
    /// 通用附件集合, NotMapped Attachments 用于记录实体对象上上传的附件
    /// </summary>
    [NotMapped]
    public virtual ICollection<Attachment>? Attachments { get; set; }
}

/// <summary>
/// 证书
/// 服务器颁发给客户端的证书, 用于校验客户端身份
/// </summary>
[Table("certificate"), DisplayName("证书")]
public abstract class Certificate<TKey> : EntitySetWithCreate<TKey>, IDateTimeRange, IEntitySet<TKey>, IEntitySetWithCreate<TKey> where TKey : IConvertible
{
    /// <summary>
    /// 用户个人信息
    /// </summary>
    [Column("personal_id"), DisplayName("个人信息编号"), MaxLength(36), Excel]
    public virtual TKey? PersonalId { get; set; }
    /// <summary>
    /// 所有者客户端编号
    /// </summary>
    [Column("client_id"), DisplayName("所有者客户端编号"), MaxLength(36), Excel]
    public virtual TKey? ClientId { get; set; }
    /// <summary>
    /// 有效期始
    /// </summary>
    [Column("start"), DisplayName("有效期始"), Field(Index = 8, Size = 16, Format = AppSettings.DEFAULT_FILE_DATE_FORMAT), Excel(Format = AppSettings.DEFAULT_DATE_FORMAT, Size = AppSettings.DEFAULT_EXCEL_COLUMN_WIDTH)]
    public virtual DateTime? Start { get; set; }
    /// <summary>
    /// 有效期至
    /// </summary>
    [Column("end"), DisplayName("有效期至"), Field(Index = 9, Size = 16, Format = AppSettings.DEFAULT_FILE_DATE_FORMAT), Excel(Format = AppSettings.DEFAULT_DATE_FORMAT, Size = AppSettings.DEFAULT_EXCEL_COLUMN_WIDTH)]
    public virtual DateTime? End { get; set; }
    /// <summary>
    /// 签名算法(证书非对称算法名称)
    /// </summary>
    [Column("signature_algorithm", TypeName = "varchar(16)"), DisplayName("签名算法"), Required, DefaultValue(EncryptionAlgorithm.RSA), Excel]
    public virtual EncryptionAlgorithm SignatureAlgorithm { get; set; } = EncryptionAlgorithm.RSA;
    /// <summary>
    /// 散列算法(证书散列算法名称)
    /// </summary>
    [Column("hash_algorithm", TypeName = "varchar(16)"), DisplayName("散列算法"), Required, DefaultValue(HashAlgorithm.SHA256), Excel]
    public virtual HashAlgorithm HashAlgorithm { get; set; } = HashAlgorithm.SHA256;
    /// <summary>
    /// 签名算法密钥长度, 默认 2048 位
    /// </summary>
    [Column("secret_length"), DisplayName("非对称密钥长度"), DefaultValue(2048), Excel]
    public virtual int SecretLength { get; set; }
    /// <summary>
    /// 证书公钥, 非对称算法公钥, 用于加密用户密钥, 保存为明文, 密钥长度 2048 位, 256 字节, BASE64 长度 392 字符
    /// </summary>
    [Column("public_key"), DisplayName("证书公钥"), Required, MaxLength(512), Excel]
    public virtual string PublicKey { get; set; } = null!;
    /// <summary>
    /// 证书内容
    /// </summary>
    [Column("content"), DisplayName("证书内容"), Excel]
    public virtual byte[]? Content { get; set; }
}